define(["zTree", "jq_request","jqform","layuiall","helper"], function (zTree,jq_request,jqform,layui,helper) {
    var form  = layui.form;
    $(".cate-tab li").click(function () {
        var index = $(this);
        var item = $(".cate-content").find(".cate-tab-item");
        index.addClass("cate-this").siblings("li").removeClass("cate-this");
        item.eq(index.index()).addClass("cate-show").siblings(".cate-tab-item").removeClass("cate-show");
    })
    
    $(".list-order").change(function () {
        var value = $(this).val();
        $.ajax({
            type:'post',
            url:$(this).attr("data-url"),
            data:{
                id:$(this).attr("data-id"),
                list_order:value
            },
            dataType:'json',
            success:function (res) {
                layer.msg(res.msg)
            },
            error:function (err) {
                layer.msg("网络错误请稍后再试")
            }
        })
    })
    
    var Controller = {
        index:function () {
            form.on('switch(status)', function(data){
                var status = 0;
                if(data.elem.checked){
                    status = 1
                }
                $.ajax({
                    type:'post',
                    data:{
                        id:data.value,
                        status:status
                    },
                    url:$(data.elem).attr("data-url"),
                    success:function (res) {
                        layer.msg(res.msg)
                    }
                })
            });
            jqform.delete()
        },
        addcategory: function () {
            level();
            uploadFile();
            jqform.init()
        },
        editcategory:function () {
            level();
            uploadFile();
            jqform.init()
        },
    };
    //层级选择
    function level() {
        $(".menuLevel").click(function () {
            var dateTime = (new Date()).getTime();
            var TreeHtml = '<ul id="MenuTree_'+dateTime+'" class="ztree"></ul>';
            var url = $(this).attr("data-url");
            var menuTreeAler = layer.open({
                type: 1,
                title: "层级选择",
                content: TreeHtml,
                area:["300px","350px"],
                shadeClose:true,
                success: function () {
                    jq_request.post(url, {
                        data: {
                            dataType: 'tree'
                        },
                        success: function (res) {
                            var zTreeObj;
                            var setting = {
                                view: {
                                    showIcon: false
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey:"id",
                                        pIdKey:"parent_id",
                                    },
                                    key:{
                                        name:"name"
                                    }
                                },
                                check: {
                                    enable: true,
                                    chkStyle: "radio",
                                    radioType: "all"
                                },
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                        nodes = zTreeObj.getCheckedNodes(true),
                                            v = "";
                                        for (var i = 0; i < nodes.length; i++) {
                                            v += nodes[i].id;
                                            $("input[name='parent_id']").val(v).siblings("input").val( nodes[i].name)
                                        }
                                        console.log(v)
                                        layer.close(menuTreeAler)
                                    },
                                    onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                        $("input[name='parent_id']").val(treeNode.id).siblings("input").val(treeNode.name)
                                        layer.close(menuTreeAler)
                                    },
                                }
                            };
                            zTreeObj = $.fn.zTree.init($("#MenuTree_"+dateTime), setting, res);
                            zTreeObj.expandAll(false);
                        }
                    })
                }
            })
        })
    }
    function uploadFile(){
        $("#test1").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/image/app/system/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    $("#app-photo").find("input[name='more[thumbnail]']").val(file[0].filepath);
                    $("#app-photo").find("img").attr("src",file[0].preview_url);
                    localStorage.removeItem("upload");
                }
            });
        });
    }
    return Controller;
});